
//--------------------------------------------------------------
// replication_clean.do — Replication cleaning script
// Project: Promoting democracy in the context of terrorism: Experimental evidence from Burkina Faso
//--------------------------------------------------------------
version 18.0
clear all
set more off
set rmsg on
cap set scheme plotplain

* --------------------------------------------------------------
* Project root (robust to where the do-file is run from)
* --------------------------------------------------------------
local thisdo = c(filename)

* Fallback if c(filename) is empty (rare)
if "`thisdo'" == "" {
    global root "`c(pwd)'"
}
else {
    * Directory containing this do-file
    local dodir : dirname "`thisdo'"
    * Project root is parent of /dofile
    local root  : dirname "`dodir'"
    global root "`root'"
}

* If someone runs from inside /dofile (extra safety)
if substr("${root}", -6, 6) == "dofile" {
    global root = substr("${root}", 1, length("${root}")-6)
}

* --------------------------------------------------------------
* Paths (relative; no machine-specific paths)
* --------------------------------------------------------------
global data   "${root}/data"
global do     "${root}/dofile"
global out    "${root}/output"
global table  "${out}/tables"
global graph  "${out}/graphs"

cap mkdir "${out}"
cap mkdir "${table}"
cap mkdir "${graph}"

cap log close _all
log using "${out}/replication_clean.log", text replace name(rep)
*--------------------------------------------------------------
* Data input
*--------------------------------------------------------------

import delimited "${data}/final_data.csv", bindquote(strict) varnames(1) maxquotedrows(100) clear

*** Pre-treatment variables

 ***-------------------------------
*** Clean data for use
***-------------------------------
// The first part will no 

label def c 1 "no" 2 "yes"
label val consent c
keep if consent==2
keep if study=="main study"
***-------------------------------
*** RECODE KEY VARIABLES
***-------------------------------

** Gender
tab gender 
recode gender (1=0 "male") (2=1 "female") (-1=.a), gen(female)
label var female "female"
fre female
**Age
tab age_1
rename age_1 age
label var age "age"
*preserve
keep if age>17
tab age

* Generate age-groups
recode age (18/25=1 "18-25")(26/35=2 "26-35")(36/45=3 "36-45")(46/55=4 "46-55")(56/100 =5 "56 and above"), ge(age_groups)
ta age_groups

** Education
fre education
label def ed 1 "No formal education" 2 "Pre-school (kindergarden)" 3 "Post-primary (general)" 4 "post-primary (technical and vocational)" 5 "Post-primary professional" 6 "secondary school (general)" 7 "secondary (technical and vocational)" 8 "University" 

label val education ed
recode education (1/2=1 "Below primary") (3/5=2 "Primary") (6/7=3 "Secondary") (8=4 "University") (-1=.), gen(educ_new)
label var educ_new "education_new"
ta educ_new

** Religion
tab religion
label def re 1 "Muslim (islam)" 2 "Catholic" 3 "Protestant" 4 "traditional/ethnic religion" 5 "none"
recode religion (1=1 "Muslism")(2=2 "Catholic")(3=3 "protestant")(4/5=4 "others")(-1=.), ge(reli_new)
label var reli_new "religion_new"
fre reli_new
*recode for descriptive statistics
recode religion (-1=.), ge(religion_new)
label val religion_new re 
fre religion_new

** Ethnicity
tab ethnicity
label def et 1 "Mossi" 2 "Fula (peulh)" 3 "Bobo" 4 "Gurma" 5 "Gurunsi" 6 "Other"
label val ethnicity et
ta ethnicity
recode ethnicity (1=1 "Mossi")(2=2 "Fula")(3=3 "Bobo")(4=4 "Gurma")(5=5 "Gurunsi")(6=6 "Other"), ge(ethnicity_new)
label var ethnicity_new "ethnicity"

** Province/town
ta province_1
ta province_2

ge city=q38_2
replace city=province_2 if missing(city)
ta city


** Important democracy
*label def de 0 "Not important at all" 10 "very important" 
tab demo_imp_pre
rename demo_imp_pre gendemoc_pre
label var gendemoc_pre "gendemoc_pre"
tab gendemoc_pre

** Political interest
ta polint
*label def po 0 "Not at all interested" 1 "not very interested" 2 "somewhat interest" 3 "very interested" -1 "I don't know"
*label drop po
*label val polint po
fre polint
recode polint (-1=.)
tab polint, nolabel
label var polint "Political interest"
recode polint (0=0)(1=1)(2=2)(3=3),ge(polinter)
ta polinter
label var polinter "Political interest"

** Turnout
*If presidential elections were held tomorrow, how likely would you vote for the current president, Captain Ibrahim Traore?
*label def 0 "Not Likely at all" 10 "Very likely"
tab vote_intent_1
rename vote_intent_1 vote_intent
tab vote_intent
label var vote_intent "Vote intention"

** Safety in the community
* label def 0 "Not at all safe" 10 "very safe"
tab safe_community_1
rename safe_community_1 safe_community
label var safe_community "Community safety"
tab safe_community

**  Emotions tratits
tab1 emotion_traits_2 emotion_traits_3 emotion_traits_5
rename emotion_traits_2 anxiety_traits
label var anxiety_traits "anxiety traits"
rename emotion_traits_3 anger_traits
label var anger_traits "anger traits"
rename emotion_traits_5 optimism_traits
label var optimism_traits "Optimism traits"

tab1 anxiety anger  optimism
su anxiety anger optimism


*Terrorisn affect
fre terror_pers_*
recode terror_pers_1 (1=1 "yes")(2=0 "no")(-1=.), ge(terror_displaced)
label var terror_displaced "Take care of displaced persons"

recode terror_pers_2 (1=1 "yes")(2=0 "no")(-1=.), ge(terror_injured)
ta  terror_injured
label var terror_injured "Being injured or having someone killed"

recode terror_pers_3 (1=1 "yes")(2=0 "no")(-1=.), ge(terror_home)
ta terror_home
label var terror_home "Leaving home due to terrorism"

*ta terror_exp
*fre terror_*
ge terror_exp=.
replace terror_exp=0 if terror_displaced==0|terror_injured==0|terror_home==0
replace terror_exp=1 if terror_displaced==1|terror_injured==1|terror_home==1
fre terror_exp
label var terror_exp "Lived experienced of terrorism"

egen terror_exp1=rowtotal(terror_displaced terror_injured terror_home)
ta terror_exp1

*--------------------------------------
** Treatment
*--------------------------------------
replace treatment="rights" if treatment=="terrorism"
tab treatment
tab treatment, nolabel

gen treat =.
replace treat = 0 if treatment == "placebo"
replace treat = 1 if treatment == "rights"
replace treat = 2 if treatment == "terrorism general"
replace treat = 3 if treatment == "terrorism Burkina"

lab def treatlbl 0 "Placebo" 1 "Rights"  2 "Terrorism general" 3 "Terrorism Burkina"
lab val treat treatlbl
tab treat
*tab treatment treat if attrition==0 & study=="main study"

* generate treatment pooled vs placebo
codebook treat
recode treat(1/3 =1)(0=0), ge(alltreat)
label var alltreat "Pooled treatments vs. Placebo"
ta alltreat treat

* generate treatment righs vs. Placebo
recode treat(1=1)(0 2 3 =0),ge(rights)
*generate treatment Burkina vs Placebo
recode treat(3=1)(0 1 2 =0),ge(Bftreat)
*generate treatment General vs Placebo
recode treat(2=1)(0 1 3 =0),ge(gentreat)
ta treat rights
ta treat Bftreat
ta treat gentreat
* Generate Burkna Faso treatment versus general terrorism treatment
recode treat(3=1)(2=0)(0 1=.), ge(bfaso_g)
xi: fre i.bfaso_g

* generate pooled terrorism treatment and rights versus placebo

codebook treat
recode treat (0=0 "Placebo")(1=1 "rights") (2 3 =2 "terrorism_right"), ge(terrorism_right)
*label var terrorism_right "Pooled terrorism treatment and rights versus placebo"
codebook terrorism_right
****-----------------------------------------
*Video infos
fre video_infos
label var video_infos "video informative"

* video_vicarious
fre video_vicarious_1
label var video_infos "video represent the country"
rename video_vicarious_1 video_vicarious

*video_social
fre video_social_1 
rename video_social_1 video_social
label var video_social "Share video infos with others"

*video_control
fre video_controls
label def vd 1 "placebo" 2 "terro_general" 3 "terro_Burkina"  4 "right"
label val video_controls vd

**** Outcomes

**  Future security
* To what extent are you concerned about the terrorisme in Burkina Faso? 
* from 0 "not concerned at all" to 10 "very much concerned"
tab post_security_1
rename post_security_1 post_security
ta post_security

** Important democracy
tab demo_imp_post_1
rename demo_imp_post_1 gendemoc_post
tab gendemoc_post

* pre-post democartic attitudes
gen democ_change = gendemoc_post - gendemoc_pre
ta democ_change

* Support for authoritarian alternatives
fre authoritarianism_*

label def au 1 "Very good" 2 "fairly good" 3 "neither good, nor bad" 4 "fairly bad" 5 "very bad"
label val authoritarianism_* au

label var authoritarianism_1 "strong leader"
label var authoritarianism_2 "military rule"
label var authoritarianism_3 "One party rule"
label var authoritarianism_4 "Charia rule"


tab1 authoritarianism_1 authoritarianism_2 authoritarianism_3 authoritarianism_4
recode authoritarianism_* (-1=.)

egen auth_support = rowmean(authoritarianism_1 authoritarianism_2 authoritarianism_3 authoritarianism_4)
sum auth_support
fre auth_support
lab var auth_support "Rejection of authoritarian alternatives"

* Policy priority
fre priority_*
gen priority_weapon =.
replace priority_weapon=priority_1 if priority_1 <=1000
label var priority_weapon "Buy more weapon"

gen priority_cohesion =.
replace priority_cohesion=priority_2 if priority_2 <=1000
label var priority_cohesion "Social cohesion"

gen priority_job =.
replace priority_job=priority_3 if priority_3 <=1000
label var priority_3 "Job creation"

tab1 priority_weapon priority_cohesion priority_job

*Policy preference
label var policy_ademoc_1 "Vote for democratic policy"
rename policy_ademoc_1 democ_po
fre democ_po
*reversed code
recode democ_po(0=10)(1=9)(2=8)(3=7)(4=6)(5=5)(6=4)(7=3)(8=2)(9=1)(10=0), ge(democ_policy)
fre democ_policy

* Donation task
label var donation_1 "Donation to democracy"
rename donation_1 donation
su donation

* Demonstration for democracy***

label var demonstrat_democracy_1 "Demonstration for democracy"
rename  demonstrat_democracy_1 demonstrat_democracy
fre demonstrat_democracy

* Democratic advantage in fighting terrorism***

label var q99_1 "Democratic advantage"
rename  q99_1 terrorism_regime
fre terrorism_regime

gen attrition  = 0
replace attrition=1 if gendemoc_post==. & auth_support==. &  democ_policy==. & terrorism_regime==. & demonstrat_democracy==. & donation==. & post_security==. & priority_weapon ==. &  priority_cohesion ==. &  priority_job ==. 

tab attrition 
**
save "${data}/BFdata.dta", replace

/****************************************************************************************
SECTION 1 — Import and prepare Burkina Faso geolocation reference (cities → coordinates)
****************************************************************************************/

* Import city-level geolocation reference (lat/lon, province, etc.)
import excel "${data}/BF_location.xlsx", sheet("data") firstrow clear
save "${data}/BF_location.dta", replace


/****************************************************************************************
SECTION 2 — Merge survey data with geolocation: attach coordinates to survey cities
****************************************************************************************/

use "${data}/BFdata.dta", clear
gen id_A = _n
order id_A
sort id_A

* Merge geolocation onto survey records (by city name)
merge m:1 city using "${data}/BF_location.dta", keep(master match) force
save "${data}/BF_merged.dta", replace


/****************************************************************************************
SECTION 3 — Import terrorism events data (2022) and prepare for spatial matching
****************************************************************************************/

import excel "${data}/BF1_2022.xlsx", sheet("Sheet1") firstrow clear
save "${data}/BF1_2022.dta", replace


/****************************************************************************************
SECTION 4 — Spatial match: link each survey location to nearby terrorism events (≤200 km)
****************************************************************************************/

use "${data}/BF_merged.dta", clear
order id_A
sort id_A

* Identify all events within 200km of each survey location (geonear output = all matches)
geonear id_A lat lng using "${data}/BF1_2022.dta", ///
    n(id_B latitude longitude) long within(200) near(0)

save "${data}/BF_geonear.dta", replace


/****************************************************************************************
SECTION 5 — Construct analysis file: keep closest event per respondent (nearest-neighbour)
****************************************************************************************/

use "${data}/BF_merged.dta", clear

* Attach distance-to-event table (one-to-many: each id_A may match multiple events)
merge 1:m id_A using "${data}/BF_geonear", keep(master match) keepusing(id_B km_to_id_B) nogen

* Bring in event attributes (id_B → terrorism event variables)
merge m:1 id_B using "${data}/BF1_2022.dta", keep(master match) nogen force

* Sort so that within each respondent, the closest event comes first
sort id_A km_to_id_B id_B
by id_A: gen one = (_n == 1)

* Sanity check: number of retained respondents after nearest-neighbour selection
count if one

order city city_ascii lat lng id_A latitude longitude province_1 province_2 q37
save "${data}/BF_merge.dta", replace

* Keep only the closest event match per respondent
keep if one == 1
save "${data}/BFdata.dta", replace
order id_A km_to_id_B one


/****************************************************************************************
SECTION 6 — Distance bands and descriptive checks (proximity distribution)
****************************************************************************************/

capture drop km50
egen km50 = cut(km_to_id_B), at(0,50,160)

tabstat km_to_id_B, by(km50) stats(N mean min max)

tab km50, gen(dist_)
summ dist_*
tab dist_1


/****************************************************************************************
SECTION 7 — Post-stratification setup: demographic groups and urban/rural (capital proxy)
****************************************************************************************/

* Age groups
tab age
recode age (18/25=1 "18-25") (26/35=2 "26-35") (36/103=3 "36 and above"), gen(age_grp)
tab age_grp
label var age_grp "Age group"

* Capital region indicator (Ouagadougou vs other provinces)
gen cap_region = (province_2 == "Ouagadougou")
tab cap_region if attrition == 0

label var female "Gender"
rename _merge _merge_old


/****************************************************************************************
SECTION 8 — Post-stratification cells: group name + group id (age × gender × region)
****************************************************************************************/

gen groupname = "middle_female_rural" if age_grp==2 & female==1 & cap_region==0
replace groupname = "middle_female_urban" if age_grp==2 & female==1 & cap_region==1
replace groupname = "middle_male_rural"   if age_grp==2 & female==0 & cap_region==0
replace groupname = "middle_male_urban"   if age_grp==2 & female==0 & cap_region==1

replace groupname = "old_female_rural" if age_grp==3 & female==1 & cap_region==0
replace groupname = "old_female_urban" if age_grp==3 & female==1 & cap_region==1
replace groupname = "old_male_rural"   if age_grp==3 & female==0 & cap_region==0
replace groupname = "old_male_urban"   if age_grp==3 & female==0 & cap_region==1

replace groupname = "young_female_rural" if age_grp==1 & female==1 & cap_region==0
replace groupname = "young_female_urban" if age_grp==1 & female==1 & cap_region==1
replace groupname = "young_male_rural"   if age_grp==1 & female==0 & cap_region==0
replace groupname = "young_male_urban"   if age_grp==1 & female==0 & cap_region==1

gen groupid = .
replace groupid = 1  if groupname=="young_female_rural"
replace groupid = 2  if groupname=="middle_female_rural"
replace groupid = 3  if groupname=="old_female_rural"
replace groupid = 4  if groupname=="young_male_rural"
replace groupid = 5  if groupname=="middle_male_rural"
replace groupid = 6  if groupname=="old_male_rural"
replace groupid = 7  if groupname=="young_female_urban"
replace groupid = 8  if groupname=="middle_female_urban"
replace groupid = 9  if groupname=="old_female_urban"
replace groupid = 10 if groupname=="young_male_urban"
replace groupid = 11 if groupname=="middle_male_urban"
replace groupid = 12 if groupname=="old_male_urban"


/****************************************************************************************
SECTION 9 — Merge population margins and generate post-stratification weights
****************************************************************************************/

* Merge population proportions (quota) by post-stratification cell
merge m:1 groupid using "${data}/population data.dta"

gen base_weight = 1
* ssc install survwgt
survwgt post base_weight, by(groupid) totvar(quota) generate(weight)
 * Data for analysis
   
 * generate data for age group (18-24, 25-34, 36 and above)


table (age_grp female)(cap_region) 
table (age_grp female)(cap_region) [aweight=weight]
table (age_grp female)(cap_region) if attrition==0  

save "${data}/finaldata.dta", replace
